package rmi.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import rmi.Product;

public class ClientGUI extends JFrame {

    public final static int STORE = 1;
    public final static int LIST = 2;
    private Client client;
    private ClientController controller;

    public ClientGUI() {
        super("Client");
        setup();
    }

    public static void main(String args[]) {
        new ClientGUI();
    }

    private void setup() {
        controller = new ClientController();

        mainPanel = new JPanel();
        mainPanel.add(createLoginPanel(controller));

        list = new JList();
        getContentPane().add(mainPanel);

        setSize(384, 512);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public void update(int screenType) {
        switch (screenType) {
            case STORE:
                mainPanel.removeAll();
                mainPanel.setLayout(new BorderLayout());
                mainPanel.add(list, BorderLayout.CENTER);
                mainPanel.add(createControlPanel(controller), BorderLayout.EAST);

                mainPanel.repaint();
                mainPanel.revalidate();
                break;
            case LIST:
                updateOrderListPanel(client.getProductList());
                break;
        }
    }

    private JPanel createLoginPanel(ClientController controller) {
        nameLabel = new JLabel("Name:");
        addressLabel = new JLabel("Address:");

        nameField = new JTextField(13);
        addressField = new JTextField("localhost:1050", 13);

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("LOGIN");
        loginButton.addActionListener(controller);

        JPanel namePanel = new JPanel();
        namePanel.setLayout(new GridLayout(2, 1));
        namePanel.add(nameLabel, RIGHT_ALIGNMENT);
        namePanel.add(nameField, LEFT_ALIGNMENT);

        JPanel addressPanel = new JPanel();
        addressPanel.setLayout(new GridLayout(2, 1));
        addressPanel.add(addressLabel, RIGHT_ALIGNMENT);
        addressPanel.add(addressField, LEFT_ALIGNMENT);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(loginButton);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3, 1));
        mainPanel.add(namePanel);
        mainPanel.add(addressPanel);
        mainPanel.add(buttonPanel);

        return mainPanel;
    }

    private JPanel createControlPanel(ClientController controller) {
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
        controlPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

        JButton listButton = new JButton("View products");
        listButton.setActionCommand("LIST");
        listButton.addActionListener(controller);

        JButton newButton = new JButton("New order");
        newButton.setActionCommand("NEW");
        newButton.addActionListener(controller);

        JButton addButton = new JButton("Add product");
        addButton.setActionCommand("ADD");
        addButton.addActionListener(controller);

        JButton summaryButton = new JButton("Get Summary");
        summaryButton.setActionCommand("SUMMARY");
        summaryButton.addActionListener(controller);

        JButton confirmButton = new JButton("Confirm");
        confirmButton.setActionCommand("CONFIRM");
        confirmButton.addActionListener(controller);

        controlPanel.add(listButton);
        controlPanel.add(newButton);
        controlPanel.add(addButton);
        controlPanel.add(summaryButton);
        controlPanel.add(confirmButton);
        controlPanel.setPreferredSize(new Dimension(160, 512));

        return controlPanel;
    }

    private void updateOrderListPanel(List<Product> productList) {
        if (productList != null) {
            try {
                DefaultListModel listModel = new DefaultListModel();

                for (Product p : productList) {
                    listModel.addElement(p.getDescription());
                }

                list.setModel(listModel);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private class ClientController implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("LOGIN")) {
                String clientName = nameField.getText();
                String serverAddress = addressField.getText();

                if (clientName != null && serverAddress != null && clientName.length() > 0 && serverAddress.length() > 0) {
                    client = new Client(clientName, serverAddress);
                    update(STORE);
                }
            } else if (e.getActionCommand().equals("LIST")) {
                update(LIST);
            } else if (e.getActionCommand().equals("NEW")) {
                client.addNewOrder();
            } else if (e.getActionCommand().equals("ADD")) {
                if(!list.isSelectionEmpty()) {
                    String inputValue = JOptionPane.showInputDialog("Please input amount");
                    if (inputValue != null) {
                        try {
                            int amount = Integer.parseInt(inputValue);
                            int selectedIndex = list.getSelectedIndex();
                            if (client.addOrderItem(amount, selectedIndex)) {
                                JOptionPane.showMessageDialog(null, "Item has been added to order", "Succes", JOptionPane.INFORMATION_MESSAGE);
                            } else {
                                JOptionPane.showMessageDialog(null, "Item could not be added to order", "Error", JOptionPane.ERROR_MESSAGE);
                            }
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(null, "That amount is not valid!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            } else if (e.getActionCommand().equals("SUMMARY")) {
                String summary = client.getOrderSummary();
                if (summary != null) {
                    JOptionPane.showMessageDialog(null, summary, "Order summary", JOptionPane.INFORMATION_MESSAGE);
                }
            } else if (e.getActionCommand().equals("CONFIRM")) {
                if (client.addConfirmation()) {
                    JOptionPane.showMessageDialog(null, "Order has been confirmed", "Cofirmation", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }
    private JPanel mainPanel;
    private JLabel nameLabel;
    private JLabel addressLabel;
    private JList list;
    private JTextField nameField;
    private JTextField addressField;
}
